#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SkinnedModel;
#endregion

namespace SkinnedModel
{

    //simple animated object
    public class SimpleAnimatedObject
    {

        protected Matrix worldMatrix;
        public Model currentModel;
        AnimationPlayer animationPlayer;
        protected AnimationClip[] clip = null;

        public SimpleAnimatedObject()
        {
            worldMatrix = Matrix.Identity;
        }

        public void InitAnimation(Model cm, ref string[] tracks)
        {
            currentModel = cm;
            SkinningData skinningData = currentModel.Tag as SkinningData;

            if (skinningData == null)
                throw new InvalidOperationException
                    ("This model does not contain a SkinningData tag.");


            // Create an animation player, and start decoding an animation clip.
            animationPlayer = new AnimationPlayer(skinningData);

            int nTakes;
            nTakes = tracks.Length;
            clip = new AnimationClip[nTakes];
            for (int i = 0; i < tracks.Length; i++)
            {
                clip[i] = skinningData.AnimationClips[tracks[i]];
            }
        }

        public Matrix GetMatrix()
        {
            return worldMatrix;
        }

        public void UpdatePlayer(TimeSpan gameTime)
        {            
            animationPlayer.Update(gameTime, true, worldMatrix);            
        }

        public ModelMeshCollection GetMeshes(){
            return currentModel.Meshes;
        }

        public Matrix[] GetSkinTransorms()
        {
            return animationPlayer.GetSkinTransforms();            
        }

        public void SetMatrix(Matrix m)
        {
            worldMatrix = m;
        }

        public void StartClip()
        {
            //ikSolver.SetPoseFromFirstKey(clip[0]);
            animationPlayer.StartClip(clip[0]);
        }

        public void StartClip(int idx)
        {
            if (idx == -1)
            {
                animationPlayer.StopClip();
            }
            else
            {
                animationPlayer.StartClip(clip[idx]);               
            }
        }

        public AnimationClip GetClip(int idx)
        {
            return clip[idx];
        }

        /*public void SetModel(Model model)
        {
            currentModel = model;
        }*/
    }


}